package com.suian.spring.thread;

public class T5ThreadWaitAndNotify {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                run();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        //启动线程
        t.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("Main 线程输出：" + i);
            Thread.sleep(1000);
            if (i == 5) {
                //需要唤醒t线程如何处理
                synchronized (T5ThreadWaitAndNotify.class) {
                    T5ThreadWaitAndNotify.class.notify();
                }
            }
        }

    }

    public static synchronized void run() throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            if (i == 2) {
                T5ThreadWaitAndNotify.class.wait();
            }

            System.out.println("x 线程输出：" + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
